#### This code loads the data downloaded from WTO, WB and processed by the authors
#### as explained in supplemental information and replication materials
#### Computes average values since the "start year" (of democracy) for each country
#### Produces figure 1 in the paper 

#### Note that data for Chile and Uruguay's service exports was not available in the WB records
#### Data for Chile was obtained from that country's Central Bank. Data for Uruguay was unobtainable,
#### so it was imputed by the authors


#####
load("~/Dropbox/Data/Paper-VotoEconomico/DATA/data_fundamentals.RData")

### Classify the countries #####
s1 <- data.frame(as.table(by(d$ServiceExports[d$Year>=d$startyear],d$country[d$Year>=d$startyear] ,mean,na.rm=T)))

s2 <- data.frame(as.table(by(d$CommDep[d$Year>=d$startyear],d$country[d$Year>=d$startyear] ,mean,na.rm=T)))

names(s1) <- names(s2) <- c("Country","v")

s1$v[s1$Country=="uru"] <- mean(s1$v[s1$Country=="arg"],s1$v[s1$Country=="bra"])#IMPUTE URY
ss <- merge(s1,s2,by="Country",suffixes=c("IRexposure","CommDep"))
ss$Country <- toupper(ss$Country)

pdf(file="Figures/fig-fundamentals.pdf", width = 6, height = 6)
par(mfrow=c(1,1),mar=c(4.5,4.5,1,1))
plot(ss$vIRexposure,ss$vCommDep,type="n",
	xlab="",ylab="",bty="n"
	#xlim=c(0,.5),ylim=c(.1,.9)
	)
mtext(side=1,line=2,text="Debt Service/Exports")
mtext(side=1,line=3,text="(Exposure to International Interest Rates)",cex=.8)
mtext(side=2,line=3.3,text="Commodities/Total Exports")
mtext(side=2,line=2.3,text="(Exposure to Commodity Prices)",cex=.8)
polygon(x=c(.13,.6,.6,.2),
		y=c(.9,.1,.9,.9),
		col=gray(.85),border=NA)
text(ss$vIRexposure,ss$vCommDep,labels=ss$Country,cex=1)
#abline(.9,-1.6)
dev.off()
